'use strict'; //严格模式

// 引入所有路由文件


const express = require('express');
const path = require('path');

const mutipart= require('connect-multiparty');
const mutipartMiddeware = mutipart();

//引入路由文件
const classRouter = require("./classRouter");
const teachRouter=require("./teachRouter");
const courseRouter=require("./courseRouter")
const studentRouter=require("./studentRouter");
const adminRouter=require("./adminRouter");
const workRouter=require('./workRouter');
const noticeRouter=require("./noticeRouter");
const messageRouter=require('./messageRouter');
const mainRouter=require('./mainRouter');

// 学生端路由
const stuRouter=require('./stuRouter');
const stu_courseRoter=require('./stu_courseRoter');



// 教师端路由

const teaRouter=require('./teaRouter');
const teaClassRouter=require('./teaClassRouter');
const teaStuRouter=require('./teaStuRouter');
const teaMessRouter=require('./teaMessRouter');
const teaWorkRouter=require('./teaWorkRouter');
const teaNoweRouter=require('./teaNoweRouter');


// 系统配置


const systemRouter=require('./systemRouter');







const indentRouter=require('./indentRouter');

const stu_informRouter=require('./stu_informRouter');

const stu_indentRouter=require('./stu_indentRouter');

const stu_workRouter=require('./stu_workRouter');


const base = require(path.join(__dirname,"../controllers/baseController.js"));

// 引入数据库文件
const db = require(path.join(__dirname,"../configs/database.js"));


//设置网站路由
var Router = function(app){
  // 设置主路由
  
app.get("*",function(req,res,next){

        if(req.url == "/main/signin" || req.url == "/main/signup" || req.url == "/main/vcode"){

          
            if(req.cookies.auto != undefined && req.cookies.admin_id != undefined)
            {
              var where = `admin_id = ${req.cookies.admin_id}`;

              db.table('admin').where(where).find().then(function(data){

                if(JSON.stringify(data) == "{}")
                {
                    res.cookie('auto',undefined);
                    res.cookie('admin_id',undefined);
                    next();
                }else{
                    req.session.admin = data;
                    res.redirect('/sys/system');
                }

              }).catch(function(err){
                if(err)
                {
                  res.cookie('auto',undefined);
                  res.cookie('admin_id',undefined);
                  next();
                }
              });
            }else{
                next();  //直接跳过
            }


        }else{
          console.log(req.session);
            if(req.session.admin == undefined)
            {
                req.session.admin = null;
                res.redirect('/main/signin');
                return false;
            }else{
                next();
            }
        }
    });


  app.use('/main',mainRouter);
  // /class/class_list
  // /class/class_add
  app.use('/class',classRouter);

  //教师的路由
  app.use('/teach',teachRouter);
  // 课程的路由
  app.use('/course',courseRouter);
  // 学生的路由
  app.use('/student',studentRouter);

  // 管理员的路由
  app.use('/admin',adminRouter);

  app.use('/work',workRouter);

  app.use('/notice',noticeRouter);

  app.use('/message',messageRouter);

  app.use('/indent',indentRouter);

  app.use('/stu',stuRouter);

  app.use('/ind',stu_indentRouter);

  app.use('/stu_work',stu_workRouter);

  app.use('/inform',stu_informRouter);
  
  app.use('/tea',teaRouter);
  app.use('/teaclass',teaClassRouter);

  app.use('/teaStu',teaStuRouter);

  app.use('/teaMess',teaMessRouter);
  
  app.use('/teaWork',teaWorkRouter);

  app.use('/teaNowe',teaNoweRouter);

  app.use('/sys',systemRouter);

  app.use('/stucur',stu_courseRoter);



 // 把图片存进本地


 app.use(mutipart({ uploadDir:path.join(__dirname,"/static/uploads") }));


};

module.exports = Router;





